/**********************************************
** @Des: This file ...
** @Author: xiaomeng79
** @Date:   17-11-17 上午9:57
***********************************************/
package controllers

import (
	"time"
	"github.com/xiaomeng79/go-rbac/models"
	"github.com/xiaomeng79/go-rbac/libs"
)

type RoleController struct {
	BaseController
}

/**
添加
 */
func (this *RoleController) Add() {
	if this.isPost() {//post请求
		Role := new(models.Role)
		Role.Role_name = libs.FilterStr(this.GetString("role_name"))
		Role.Created_at = time.Now().Unix()
		Role.Updated_at = time.Now().Unix()
		Role.Is_usable = 1
		pri_ids := make([]int,0)
		this.Ctx.Input.Bind(&pri_ids,"pri_ids")
		if err := Role.Add(pri_ids);err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
			this.ajaxMsg("添加成功",MSG_OK)

	} else {
		//权限列表
		Privilege := new(models.Privilege)
		Pridata := Privilege.Lst()
		this.Data["Title"] = "添加角色"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "角色列表"
		this.Data["Currentname"] = "添加角色"
		this.Data["Pridata"] = Pridata
		this.display()
	}
}

/**
列表
 */
func (this *RoleController) Lst() {
	Role := new(models.Role)
	result := Role.Lst()

	this.Data["Title"] = "角色列表"
	this.Data["Navurl"] = "lst"
	this.Data["Navname"] = "角色列表"
	this.Data["Currentname"] = "角色列表"
	this.Data["Data"] = result
	this.display()
}

/**
修改
 */
func (this *RoleController) Edit() {
	if this.isPost() {//post请求
		Role := new(models.Role)
		Role.Id,_ = this.GetInt("id")
		Role.Role_name = libs.FilterStr(this.GetString("role_name"))
		Role.Updated_at = time.Now().Unix()
		pri_ids := make([]int,0)
		this.Ctx.Input.Bind(&pri_ids,"pri_ids")
		if err := Role.Edit(pri_ids,"role_name","updated_at");err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
		this.ajaxMsg("修改成功",MSG_OK)

	} else {
		Role_id,_ := this.GetInt("id")
		res,err := models.GetRoleById(Role_id)
		if err != nil {
			this.redirect(this.URLFor("RoleController.Lst"))
		}
		//权限列表
		Privilege := new(models.Privilege)
		Pridata := Privilege.Lst()
		this.Data["Data"] = res
		this.Data["Title"] = "修改角色"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "角色列表"
		this.Data["Currentname"] = "修改角色"
		this.Data["Pridata"] = Pridata
		this.display()
	}
}

/**
删除
 */
func (this *RoleController) Del() {
		id, _ := this.GetInt("id")
		Role := new(models.Role)
		//软删除
		if err := Role.Del(id); err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}

		this.ajaxMsg("删除成功", MSG_OK)
}